package com.jilintongyi.jlcrtvu.controller;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(tags = { "页面控制器" })
public class PageController {
	@Value("${security.page.admin.has-authority}")
	private Boolean adminHasAuthority;

	@GetMapping("/")
	@ApiOperation(value = "首页跳转", notes = "首页跳转")
	public String index() {
		return "/index";
	}

	@GetMapping("/index")
	@ApiOperation(value = "首页跳转", notes = "首页跳转")
	public String index2() {
		return "/index";
	}

	@GetMapping("/login")
	@ApiOperation(value = "登录页面跳转", notes = "登录页面跳转")
	public String login() {
		return "/login";
	}

	@GetMapping("/admin/{path}")
	@ApiOperation(value = "跳转到/admin/*.html", notes = "跳转到/admin/*.html")
	public String admin(@PathVariable String path) {
		if (!adminHasAuthority) {
			return "/admin/" + path;
		}
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> grantedAuthorityCollection = authentication.getAuthorities();
		for (Iterator<?> iterator = grantedAuthorityCollection.iterator(); iterator.hasNext();) {
			GrantedAuthority grantedAuthority = (GrantedAuthority) iterator.next();
			if (grantedAuthority.getAuthority().equals("/admin/" + path)) {
				return "/admin/" + path;
			}
		}
		return "/admin/403";
	}

	@GetMapping("/admin/{path}/{path2}")
	@ApiOperation(value = "跳转到/admin/*/*.html", notes = "跳转到/admin/*/*.html")
	public String admin2(@PathVariable String path, @PathVariable String path2) {
		if (!adminHasAuthority) {
			return "/admin/" + path + "/" + path2;
		}
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> grantedAuthorityCollection = authentication.getAuthorities();
		for (Iterator<?> iterator = grantedAuthorityCollection.iterator(); iterator.hasNext();) {
			GrantedAuthority grantedAuthority = (GrantedAuthority) iterator.next();
			if (grantedAuthority.getAuthority().equals("/admin/" + path + "/" + path2)) {
				return "/admin/" + path + "/" + path2;
			}
		}
		return "/admin/403";
	}
}
